//
//  MeSourceCenterViewController.m
//  ChinaA
//
//  Created by Mxionlly on 2018/11/22.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "MeSourceCenterViewController.h"
#import "taskOverviewService.h"
#import "MeCenterViewCell.h"

@interface MeSourceCenterViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic,strong)UITableView    *tableView;
@property (nonatomic,strong)dataSourceCenterInfo *model;
@property (nonatomic,strong)NSString *exerciseNumber;
@end

@implementation MeSourceCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"行权中心";
    [self loadData];
    [self.view addSubview:self.tableView];
}

-(void)loadData{
    [taskOverviewService getExerciseOrder:nil success:^(dataSourceModel *response, BOOL success) {
        if (success) {
             self.model = response.data;
            [self.tableView reloadData];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        
    }];
}

#pragma mark - init
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(30, distanceHeight+30, KScreenWidth-60, KScreenHeight-distanceHeight-30) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
        _tableView.scrollEnabled = YES;
        _tableView.sectionHeaderHeight = 0.00001;
        _tableView.sectionFooterHeight = 0.00001;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

#pragma mark - UITableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 100)];
    UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    footerButton.frame = CGRectMake(30, 50, KScreenWidth - 120, 40);
    footerButton.backgroundColor = COLOR_main;
    footerButton.layer.masksToBounds = YES;
    footerButton.layer.borderWidth = 1.0f;
    footerButton.layer.borderColor = [UIColor clearColor].CGColor;
    footerButton.layer.cornerRadius = 20.0f;
    footerButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [footerButton setTitle:@"确认行权" forState:UIControlStateNormal];
    [footerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footerButton addTarget:self action:@selector(sumbitAction) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:footerButton];
    return footerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeCenterViewCell *cell = [MeCenterViewCell cellWithTableView:tableView];
    cell.textField.userInteractionEnabled = NO;
    cell.textField.delegate = self;
    switch (indexPath.row) {
        case 0:
        {
            cell.lbtitle.text = [NSString stringWithFormat:@"总资产：%@",self.model.CoinNumber];
            cell.lbcontent.text = @"china-A";
        }
            break;
        case 1:
        {
            cell.lbtitle.text = [NSString stringWithFormat:@"已行权个数：%@",self.model.AlreadyExerciseNumber];
            cell.lbcontent.text = @"册";
        }
            break;
        case 2:
        {
            cell.lbtitle.text = [NSString stringWithFormat:@"未行权个数：%@",self.model.NotExerciseNumber];
            cell.lbcontent.text = @"册";
        }
            break;
        case 3:
        {
            cell.lbtitle.text = [NSString stringWithFormat:@"申请行权："];
            cell.textField.userInteractionEnabled = YES;
            cell.textField.placeholder = @"可填写未行权个数";
            cell.lbcontent.text = @"册";
        }
            break;
        default:
            break;
    }
    return cell;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.exerciseNumber = textField.text;
}

-(void)sumbitAction
{
    [self.view endEditing:YES];
    if (![Check IsABChinese:self.exerciseNumber] || self.exerciseNumber.length == 0) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的行权个数"];
        return;
    }
    if ([self.exerciseNumber integerValue] > [self.model.NotExerciseNumber integerValue]) {
        [DKProgressHUD showErrorWithStatus:@"行权个数不够"];
        return;
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setField:self.exerciseNumber value:@"exerciseNumber"];
    [taskOverviewService getAddExerciseOrder:dict success:^(BaseModel *response, BOOL success) {
        if (success) {
            [DKProgressHUD showSuccessWithStatus:@"成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
